﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EasySite.Models;

namespace EasySite.Services.Implementations
{
    public class PageService : ServiceBase, IPageService
    {
        #region Implementation of IPageService

        public Page GetPage(int id)
        {
            return DataContext.Pages.FirstOrDefault(p => p.ID == id);
        }

        public Page GetPage(string url)
        {
            return DataContext.Pages.FirstOrDefault(p => p.Url == url);
        }

        public bool CreatePage(Page page)
        {
            page.CreatedDate = DateTime.Now;
            page.Deleted = false;
            page.ReadsCount = 0;
            page.UpdatedDate = null;
            DataContext.Pages.Add(page);
            return base.TrySaveChanges();
        }

        public bool UpdatePage(Page page)
        {
            var dbPage = GetPage(page.ID);
            dbPage.UpdateValues(page);
            return base.TrySaveChanges();
        }

        public bool DeletePage(int id)
        {
            var page = GetPage(id);
            page.Deleted = true;
            return base.TrySaveChanges();
        }

        #endregion
    }
}
